package light.mvc.service.item.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import light.mvc.dao.BaseDaoI;
import light.mvc.model.item.TchemicalExam;
import light.mvc.model.item.Tmedicine;
import light.mvc.page.base.PageFilter;
import light.mvc.page.item.ChemicalExam;
import light.mvc.page.item.Medicine;
import light.mvc.service.item.MedicineServiceI;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MedicineServiceImpl implements MedicineServiceI {

	@Autowired
	private BaseDaoI<Tmedicine> medicineDao;

	@Override
	public void add(Medicine medicine) {
		Tmedicine t = new Tmedicine();
		BeanUtils.copyProperties(medicine, t);
		medicineDao.save(t);
	}

	@Override
	public void delete(Integer id) {
		Tmedicine t = medicineDao.get(Tmedicine.class, id);
		medicineDao.delete(t);
	}

	@Override
	public void edit(Medicine medicine) {
		Tmedicine t = medicineDao.get(Tmedicine.class,medicine.getId());
		BeanUtils.copyProperties(medicine, t);
		medicineDao.update(t);
	}

	@Override
	public Medicine get(Integer id) {
		Tmedicine t = medicineDao.get(Tmedicine.class,id);
		Medicine medicine = new Medicine();
		BeanUtils.copyProperties(t, medicine);
		
		return medicine;
	}
	
	@Override
	public List<Medicine> dataGrid(Medicine medicineSome, PageFilter ph) {
		List<Medicine> medicines = new ArrayList<Medicine>();
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from Tmedicine t ";
		List<Tmedicine> list = medicineDao.find(hql + whereHql(medicineSome, params) + orderHql(ph), params, ph.getPage(), ph.getRows());
		for (Tmedicine t : list) {
			Medicine medicine = new Medicine();
			BeanUtils.copyProperties(t, medicine);
			medicines.add(medicine);
		}
		return medicines;
	}
	
	@Override
	public Long count(Medicine medicine, PageFilter ph) {
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from Tmedicine t ";
		return medicineDao.count("select count(*) " + hql + whereHql(medicine, params), params);
	}
	
	private String whereHql(Medicine medicine, Map<String, Object> params) {
		String hql = "";
		if (medicine != null) {
			hql += " where 1=1 ";
			if (medicine.getName()!= null) {
				hql += " and t.name like :name";
				params.put("name", "%" + medicine.getName() + "%");
			}
		}
		return hql;
	}

	private String orderHql(PageFilter ph) {
		String orderString = "";
		if ((ph.getSort() != null) && (ph.getOrder() != null)) {
			orderString = " order by t." + ph.getSort() + " " + ph.getOrder();
		}
		return orderString;
	}
	
	@Override
	public Medicine getByName(Medicine medicineSome) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("name", medicineSome.getName());
		Tmedicine t = medicineDao.get("from Tmedicine t where t.name = :name",params);
		Medicine medicine = new Medicine();
		if(t!=null){
			BeanUtils.copyProperties(t, medicine);
		}else{
			return null;
		}
		return medicine;
	}
	
	@Override
	public List<Medicine> list() {
		List<Medicine> medicines = new ArrayList<Medicine>();
		String hql = " from Tmedicine t ";
		List<Tmedicine> list = medicineDao.find(hql);
		for (Tmedicine t : list) {
			Medicine medicine = new Medicine();
			BeanUtils.copyProperties(t, medicine);
			medicines.add(medicine);
		}
		return medicines;
	}
}
